﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Yule8xCommon.Applib
{
    /// <summary>
    /// 序列号通用类
    /// </summary>
    public class SerialNoUtil
    {
        public static string MakeTradeSerialNo(int uid, double amount, string timestamp, string encrypt_key, int random=9999)
        {
            // 签名加密算法 uid+amount+timestamp+key
            // 序列号:签名+随机数4位
            if (string.IsNullOrEmpty(encrypt_key))
            {
                encrypt_key = AppInfo.EncryptKey;
            }
            if (string.IsNullOrEmpty(timestamp))
            {
                timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:ii:ss");
            }
            string sign = MD5Util.GetShortMD5(string.Format("{0}{1}{2}{3}", uid, amount, timestamp, AppInfo.EncryptKey));
            if (random>0)
            {
                Random rnd = new Random();
                sign = sign + rnd.Next(random); // 16-20位
            }
            return sign;
        }
    }
}